<div class="form mg10">
  <form method="post" action="saveDeliverAddressPost" id="form" class="pd10">
    <div class="form-item clearfix">
      <label>收货人</label>
      <div>
        <input type="text" name="name" placeholder="请输入收货人姓名" value="{$deliverAddress.name}">
      </div>
    </div>
    <div class="form-item clearfix">
      <label>手机号码</label>
      <div>
        <input type="tel" name="tel" placeholder="填写正确手机号码" value="{$deliverAddress.tel}">
      </div>
    </div>
    <div class="form-item clearfix">
      <label>省份</label>
      <div>
        <select name="province" id="province">{$deliverAddress.provinceOptions}</select>
      </div>
    </div>
    <div class="form-item clearfix">
      <label>城市</label>
      <div>
        <select name="city" id="city">{$deliverAddress.cityOptions}</select>
      </div>
    </div>
    <div class="form-item clearfix">
      <label>地区</label>
      <div>
        <select name="area" id="area">{$deliverAddress.areaOptions}</select>
      </div>
    </div>
    <div class="form-item clearfix">
      <label>详细地址</label>
      <div>
        <textarea name="address">{$deliverAddress.address}</textarea>
      </div>
    </div>
    
    <div class="submit">
     <label></label>
     <input type="hidden" name="id" value="{$deliverAddress.id}">
     <button type="submit" class="btn btn-blue mb10">保存收货地址</button>
    </div>
  </form>
 </div>
 
 <script type="text/javascript">
   (function(){
     var oProvince = document.getElementById( "province" ),
         oCity = document.getElementById( "city" ),
         oArea = document.getElementById( "area" ),
         sid = {$sid};
     oProvince.addEventListener( "change", getCity, false );
     oCity.addEventListener( "change", getArea, false );
     function getCity() {
       var p = this.value;
       GH.ajax( "__CONTROLLER__/getCityPost", {code: p, sid: sid}, function(data){
         oCity.innerHTML = data;
       }, "post" );
     }
     function getArea() {
       var c = this.value;
       GH.ajax( "__CONTROLLER__/getAreaPost", {code: c, sid: sid}, function(data){
         oArea.innerHTML = data;
       }, "post" );
     }
     
     var form = document.getElementById ( "form" ),
          formInput = form.getElementsByTagName ( "input" ),
          formSelect = form.getElementsByTagName ( "select" );
       
     form.onsubmit = function(){
       if ( check() ) {
         var data = GH.serialize( "form" );
         data.sid = {$sid};
         GH.ajax ( "__CONTROLLER__/saveDeliverAddressPost", data, function(data){
           GH.showMessage( data.msg );
           if (data.error == 0 ) {
             window.location = document.referrer;
           }
         }, "post" );
       }
       return false;
     }
       
     //检查表单
     function check() {
        var check = true;
        for( var i=0,inputSelf; inputSelf=formInput[i]; i++ ) {
          if( inputSelf.getAttribute( "type" )== "hidden" ) { return true; }
          if( trim(inputSelf.value) == "" ) {
            GH.showMessage("请填写完整信息");
            inputSelf.value = "";
            check = false;
            return false;
          }
        }
        for( var i=0,selectSelf; selectSelf=formSelect[i]; i++ ) {
          if( trim(selectSelf.value) == "" ) {
            GH.showMessage("请选择收货地址");
            check = false;
            return false;
          }
        }
        return check;
     }

     //去除首尾空格
     function trim( str ) {
       return str.replace(/(^\s*)|(\s*$)/g, "");
     }
     
   })()
 </script>